package io.github.zeleven.daysmatter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import static java.lang.Math.abs;

/**
 * Created by user on 17-8-29.
 */

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolder> {

    private List<Event> dataset;
    private Context mContext;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView eventContentText;
        TextView eventDate;
        TextView eventDaysCount;
        TextView beforeDaysText;

        public ViewHolder(final View itemView) {
            super(itemView);
            eventContentText = itemView.findViewById(R.id.event_content_text);
            eventDate = itemView.findViewById(R.id.event_date);
            eventDaysCount = itemView.findViewById(R.id.event_days_count);
            beforeDaysText = itemView.findViewById(R.id.before_days_text);
        }
    }

    public EventAdapter(List<Event> dataset) {
        this.dataset = dataset;
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        if(mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.row_recyclerview, parent, false);
        final ViewHolder holder = new ViewHolder(view);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Event event = dataset.get(position);
                Intent intent = new Intent(mContext, EventDetailActivity.class);
                intent.putExtra("eventId", event.getId());
                intent.putExtra("eventContent", event.getEventContent());
                intent.putExtra("eventTargetDate", event.getTargetDate().getTime());
                intent.putExtra("eventDaysCount", Utility.getDateInterval(event.getTargetDate()));
                mContext.startActivity(intent);
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Event event = dataset.get(position);
        holder.eventContentText.setText(event.getEventContent());
        if(event.getTargetDate() != null) {
            holder.eventDate.setText(new SimpleDateFormat("yyy-MM-dd").
                    format(event.getTargetDate()));
        }
        Calendar c = Calendar.getInstance();
//        long duration = getDateDuration(c.getTime(), event.getTargetDate());
        long duration = Utility.getDateInterval(event.getTargetDate());
        String text = "";
        if(duration < 0) {
            text = "已经";
        } else if(duration > 0) {
            text = "还有";
        }
        holder.beforeDaysText.setText(text);
        holder.eventDaysCount.setText(Long.toString(abs(duration)));
    }

    @Override
    public int getItemCount() {
        return dataset.size();
    }
}
